//
//  ScrollViewLayer.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/7/13.
//
//

#include "ScrollViewLayer.h"
#include "StaticVariables.h"
#include "SceneManager.h"
bool ScrollViewLayer::init(){
    SwipeGestureLayer::init();
    
    currentPage = 0;
    
    pageArray = CCArray::create();
    pageArray->retain();
        setTouchEnabled(true);
    isDoingAnimation = false;
    return true;
}

ScrollViewLayer* ScrollViewLayer::create(int numOfPages){
    ScrollViewLayer* scrollView = ScrollViewLayer::create();
    scrollView->setUpPages(numOfPages);
    return scrollView;
}


void ScrollViewLayer::setUpPages(int numOfPages){
    numberOfPages = numOfPages;
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double pageWidth = winSize.width * 0.75f;
    double pageHeight = pageWidth ;
    double offset = pageWidth* 0.05f;
    distancePerPage = pageWidth+((winSize.width - 2*offset)-pageWidth)/2;
    
    for(int i=0;i<numOfPages;i++){
        MyPageButton* pageButton = MyPageButton::create("page_button.png", (winSize.width-pageWidth)/2 +distancePerPage*i, (winSize.height-pageHeight)/2, pageWidth, pageHeight);
        CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(ScrollViewLayer::onTapAtPage), CCInteger::create(i));
        pageButton->addCallback(callback);
        this->addChild(pageButton);
        pageArray->addObject(pageButton);
    }

}


CCRect ScrollViewLayer::getScrollingArea(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    MyPageButton* pageZero = getPageAt(0);
    return CCRectMake(0, pageZero->getPositionY(), winSize.width, pageZero->getBoundingRect().size.height);
}


void ScrollViewLayer::onTapAtPage(cocos2d::CCObject *sender){
//    int tapIndex = ((CCInteger*)sender)->getValue();
//    CCLOG("tap at %i",tapIndex);
//    switch (tapIndex) {
//        case 0:{
//            CCTransitionFade* transition = CCTransitionFade::create(TRANSITION_DURATION, SceneManager::sharedInstance()->staticTapScene, ccc3(0, 0, 0));
//            CCDirector::sharedDirector()->replaceScene(transition);
//        }
//            break;
//            
//        case 1:{
//            CCTransitionFade* transition = CCTransitionFade::create(TRANSITION_DURATION, SceneManager::sharedInstance()->hideAndSeekScene, ccc3(0, 0, 0));
//            CCDirector::sharedDirector()->replaceScene(transition);
//        }
//            break;
//    }
}
void ScrollViewLayer::goToPrev(){
    if(isDoingAnimation){
        return;
    }
    if(currentPage-1>=0){
        isDoingAnimation=true;
        movePagesBy(ccp(distancePerPage,0));
        currentPage--;
        didBeginMovingPages();
    }
}
void ScrollViewLayer::goToNext(){
    if(isDoingAnimation){
        return;
    }
    if(currentPage+1<numberOfPages){
        isDoingAnimation=true;
        movePagesBy(ccp(-distancePerPage,0));
        currentPage++;
        didBeginMovingPages();
    }
}

void ScrollViewLayer::goToPage(int page){
    if(isDoingAnimation){
        return;
    }
    int prePage = currentPage;
    if(page>=numberOfPages){
        currentPage = numberOfPages-1;
    }
    if(page<0){
        currentPage = 0;
    }
    isDoingAnimation = true;
    movePagesBy(ccp((prePage-currentPage)*distancePerPage,0));
    didBeginMovingPages();
    
}

void ScrollViewLayer::didBeginMovingPages(){
    
}

MyPageButton* ScrollViewLayer::getPageAt(int pageIndex){
    MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(pageIndex);
    return pageButton;
}

void ScrollViewLayer::movePagesBy(cocos2d::CCPoint point){
    for(int i=0;i<numberOfPages;i++){
        MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(i);
        CCMoveBy* moveAction = CCMoveBy::create(0.2f, point);
        if(i==numberOfPages-1){
            CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(ScrollViewLayer::didFinishMovingPages), pageButton);
            pageButton->runAction(CCSequence::create(moveAction,callback,NULL));
        }else{
            pageButton->runAction(moveAction);
        }
        
    }
}

void ScrollViewLayer::didFinishMovingPages(cocos2d::CCObject *sender){
    isDoingAnimation = false;
}

void ScrollViewLayer::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    SwipeGestureLayer::ccTouchesBegan(touches, pEvent);
    for(int i=0;i<numberOfPages;i++){
        MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(i);
        pageButton->handleTouchesBegan(touches, pEvent);
    }

    
}

void ScrollViewLayer::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    //CCPoint touchPosition = MyUtilities::getCoCosPointFromTouches(touches);
    SwipeGestureLayer::ccTouchesMoved(touches, pEvent);
    for(int i=0;i<numberOfPages;i++){
        MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(i);
        pageButton->handleTouchesMoved(touches, pEvent);
    }
}

void ScrollViewLayer::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    SwipeGestureLayer::ccTouchesEnded(touches, pEvent);
    if(isDoingAnimation){
        for(int i=0;i<numberOfPages;i++){
            MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(i);
            pageButton->resetState();
        }
        return;
    }
    for(int i=0;i<numberOfPages;i++){
        MyPageButton* pageButton = (MyPageButton*)pageArray->objectAtIndex(i);
        pageButton->handleTouchesEnded(touches, pEvent);
    }
    
    
}

